//
//  CycleView.m
//  quxiangtao
//
//  Created by xbmios on 2018/7/4.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "CycleView.h"
#import "NewPagedFlowView.h"
#import <AlipaySDK/AlipaySDK.h>
#import <AlibcTradeSDK/AlibcTradeSDK.h>
#import <AlibcTradeBiz/AlibcTradeBiz.h>
#import <AlibcTradeSDK/AlibcTradePageFactory.h>
#import <AlibcTradeSDK/AlibcTradeShowParams.h>
#import "ALiTradeSDKShareParam.h"
#import "JDKeplerManager.h"
@interface CycleView ()<NewPagedFlowViewDelegate, NewPagedFlowViewDataSource>

@property (strong, nonatomic) NewPagedFlowView *flowView;

//@property (strong, nonatomic) NSMutableArray *imageArray;

@property (strong, nonatomic) NSArray *cycleDataArr;

@end

@implementation CycleView

//- (instancetype)initWithImageArr:(NSMutableArray *)imageArray {
//
//    if (self == [super init]) {
//
////        self.imageArray = imageArray;
//
//        [self loadCycleData];
//
//        [self setupUI];
//
//    }
//
//    return self;
//
//}

- (instancetype)init {
    
    if (self == [super init]) {
        
        //        self.imageArray = imageArray;

        
        [self setupUI];
        
    }
    
    return self;
}

- (void)setSelectIndexBanner:(NSInteger)selectIndexBanner {
    _selectIndexBanner = selectIndexBanner;
    [self loadCycleData:selectIndexBanner];
}

- (void)loadCycleData:(NSInteger)index {
    
    NSString *str = [NSString stringWithFormat:@"{\"type\" : \"%@\"}", [RSA encryptString:index==4?@"2":@"1" publicKey:PublicKey]];
    
    [PPNetworkHelper POST:@"build/banner/bannerList" parameters:@{@"json" : str} success:^(id responseObject) {
        
        NSLog(@"轮播图%@",responseObject);
        
        if ([responseObject[@"code"] integerValue] == 1) {
            
            /*
             bannerurl = ;
             app = 1; // 1 跳转到自营 ！=1 跳转h5
             pictureurl = http://shop.quxiangtao.com/public/static/qxtgoodspic/uploads/images/20180110/6d3ddf308ad6d7908492d791f6f4de54.png;
             classid = 1;
             */
            
            self.cycleDataArr = [NSArray arrayWithArray:responseObject[@"data"]];
            
            [self.flowView reloadData1];
            
        }else {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:responseObject[@"msg"]];
        }
        
    } failure:^(NSError *error) {
        
        if (error) {
            
            [[KYAlertView sharedInstance] showAlertViewWithMessage:Tip];
        }
    }];
}

- (void)setupUI {
    
    NewPagedFlowView *pageFlowView = [[NewPagedFlowView alloc] init];
    self.flowView = pageFlowView;
    pageFlowView.frame = CGRectMake(0, 0, WIDTH, 150);
    pageFlowView.delegate = self;
    pageFlowView.dataSource = self;
    pageFlowView.minimumPageAlpha = 0.1;
    pageFlowView.orientation = NewPagedFlowViewOrientationHorizontal;
    pageFlowView.isOpenAutoScroll = YES;
//    [pageFlowView reloadData];
    [self addSubview:pageFlowView];
    
}

#pragma mark NewPagedFlowView Delegate
- (CGSize)sizeForPageInFlowView:(NewPagedFlowView *)flowView {
    
    return CGSizeMake(WIDTH - 60, 150);
    
}


- (void)didSelectCell:(UIView *)subView withSubViewIndex:(NSInteger)subIndex {
    
    NSLog(@"点击了第%ld张图",(long)subIndex + 1);
    
    NSDictionary *dict = self.cycleDataArr[subIndex];
    NSString *type = dict[@"app"];
    
    if ([type integerValue] == 1) {
        // 跳转至自营
        BaseTabbarViewController *Vc = [BaseTabbarViewController sharedMainTabBarVC];
        Vc.selectedIndex = 1;
        UIWindow *keywindow = [[UIApplication sharedApplication].windows firstObject];
        keywindow.rootViewController = Vc;
        [keywindow makeKeyAndVisible];
        
    } if ([type integerValue] == 4) {
        [[JDKeplerManager sharedManager] jumpJDUrl:dict[@"bannerurl"] Controller:self.parentVC];
    }  else {
        
        //根据链接打开页面
        id<AlibcTradePage> page = [AlibcTradePageFactory page:dict[@"bannerurl"]];
        AlibcTradeTaokeParams *taoKeParams=[[AlibcTradeTaokeParams alloc] init];
        taoKeParams.pid= nil;
        //打开方式
        AlibcTradeShowParams* showParam = [[AlibcTradeShowParams alloc] init];
        showParam.openType = AlibcOpenTypeAuto;
        [[AlibcTradeSDK sharedInstance].tradeService show: self.parentVC.navigationController page:page showParams:showParam taoKeParams: nil trackParam: [self customParam] tradeProcessSuccessCallback:nil tradeProcessFailedCallback:nil];
    }
}

-(AlibcTradeTaokeParams*)taokeParam{
    if ([ALiTradeSDKShareParam sharedInstance].isUseTaokeParam) {
        AlibcTradeTaokeParams *taoke = [[AlibcTradeTaokeParams alloc] init];
        taoke.pid =[[ALiTradeSDKShareParam sharedInstance].taoKeParams objectForKey:@"pid"];
        taoke.subPid = [[ALiTradeSDKShareParam sharedInstance].taoKeParams objectForKey:@"subPid"];
        taoke.unionId = [[ALiTradeSDKShareParam sharedInstance].taoKeParams objectForKey:@"unionId"];
        taoke.adzoneId = [[ALiTradeSDKShareParam sharedInstance].taoKeParams objectForKey:@"adzoneId"];
        taoke.extParams = [[ALiTradeSDKShareParam sharedInstance].taoKeParams objectForKey:@"extParams"];
        return taoke;
    } else {
        return nil;
    }
    
}

- (void)didScrollToPage:(NSInteger)pageNumber inFlowView:(NewPagedFlowView *)flowView {
    
    //    NSLog(@"ViewController 滚动到了第%ld页",pageNumber);
    
}
-(NSDictionary *)customParam{
    NSDictionary *customParam=[NSDictionary dictionaryWithDictionary:[ALiTradeSDKShareParam sharedInstance].customParams];
    return customParam;

}

#pragma mark NewPagedFlowView Datasource
- (NSInteger)numberOfPagesInFlowView:(NewPagedFlowView *)flowView {
    
    return self.cycleDataArr.count;
    
}

- (PGIndexBannerSubiew *)flowView:(NewPagedFlowView *)flowView cellForPageAtIndex:(NSInteger)index{
    
    PGIndexBannerSubiew *bannerView = [flowView dequeueReusableCell];
    
    if (!bannerView) {
        
        bannerView = [[PGIndexBannerSubiew alloc] init];
        bannerView.tag = index;
        bannerView.layer.cornerRadius = 4;
        bannerView.layer.masksToBounds = YES;
        
    }
    
    NSDictionary *dict = self.cycleDataArr[index];
    
    //在这里下载网络图片
    [bannerView.mainImageView sd_setImageWithURL:[NSURL URLWithString:dict[@"pictureurl"]] placeholderImage:nil];
    
//    bannerView.mainImageView.image = self.imageArray[index];
    
    return bannerView;
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
